﻿@using RazorLight
@using SmartCode
@using SmartCode.Db
@inherits TemplatePage<BuildContext>
@{
    DisableEncoding = true;
    var project = Model.Project;
    var dbSource = Model.GetDataSource<DbSource>();
    var buildTask = Model.Build;
    var table = Model.GetCurrentTable();
    var pkCol = table.PKColumn;
    var primaryKeyType = pkCol?.LanguageType ?? "int";
    var autoIncrement = table.AutoIncrement;
    var prefix = dbSource.DbProvider.ParameterPrefix;
    var colIndex = 0;
}
<?xml version="1.0" encoding="utf-8" ?>
<!--
    @{await IncludeAsync("Copyright.cshtml", Model);}
-->
<SmartSqlMap Scope="@table.ConvertedName" xmlns="http://SmartSql.net/schemas/SmartSqlMap.xsd">
    <Statements>
        <Statement Id="QueryParams">
            <Where>
            </Where>
        </Statement>
        <Statement Id="AllCols">
            @{ colIndex = 0;}
            @foreach (var col in table.Columns)
            {
                @col.Name@(colIndex == table.Columns.Count() - 1 ? "" : ",")
                colIndex++;
            }
        </Statement>
        <!--新增-->
        <Statement Id="Insert">
            INSERT INTO @table.Name
            (
            @{ colIndex = 0;}
            @foreach (var col in table.Columns)
            {
                @if (!col.AutoIncrement)
                {
                    @col.Name@(colIndex == table.Columns.Count() - 1 ? "" : ",")
                }
                colIndex++;
            }
            )
            VALUES
            (
            @{ colIndex = 0;}
            @foreach (var col in table.Columns)
            {
                @if (!col.AutoIncrement)
                {
                    @prefix@col.ConvertedName@(colIndex == table.Columns.Count() - 1 ? "" : ",")
                }
                colIndex++;
            }
            );
            @if (autoIncrement)
            {
                <text>Select Last_Insert_Id();</text>
            }
        </Statement>
        <!--删除-->
        <Statement Id="Delete">
            Delete FROM @table.Name
            Where @pkCol.Name= @($"{prefix}{pkCol.ConvertedName}")
        </Statement>
        <!--更新-->
        <Statement Id="Update">
            UPDATE @table.Name
            SET
            <Dynamic>
                @foreach (var col in table.Columns)
                {
                    if (!col.IsPrimaryKey)
                    {
                        <IsProperty Prepend="," Property="@col.ConvertedName">
                            @col.Name = @($"{prefix}{col.ConvertedName}")
                        </IsProperty>
                    }
                }
            </Dynamic>
            Where @pkCol.Name=@prefix@pkCol.ConvertedName
        </Statement>
        <!--获取数据列-->
        <Statement Id="Query">
            SELECT T.* From @table.Name T With(NoLock)
            <Include RefId="QueryParams" />
            Order By T.@pkCol.Name Desc
            <Switch Prepend="Order By" Property="OrderBy">
                <Default>
                    T.@pkCol.Name Desc
                </Default>
            </Switch>
            <IsNotEmpty Prepend="Limit" Property="Taken">?Taken</IsNotEmpty>
        </Statement>
        <!--获取分页数据-->
        <Statement Id="QueryByPage">
            Select T.* From @table.Name As T
            <Include RefId="QueryParams" />
            <Switch Prepend="Order By" Property="OrderBy">
                <Default>
                    T.@pkCol.Name Desc
                </Default>
            </Switch>
            Limit ?Offset,?PageSize
        </Statement>
        <!--获取记录数-->
        <Statement Id="GetRecord">
            Select Count(1) From @table.Name T With(NoLock)
            <Include RefId="QueryParams" />
        </Statement>
        <!--获取表映射实体-->
        <Statement Id="GetEntity">
            Select T.* From @table.Name As T
            <Where>
                <IsNotEmpty Prepend="And" Property="@pkCol.ConvertedName">
                    T.@pkCol.Name= @($"{prefix}{pkCol.ConvertedName}")
                </IsNotEmpty>
            </Where>
            Limit 1
        </Statement>
        <!--是否存在该记录-->
        <Statement Id="IsExist">
            Select Count(1) From @table.Name T With(NoLock)
            <Include RefId="QueryParams" />
        </Statement>
    </Statements>
</SmartSqlMap>